﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.User.Entity;


public partial class sysadmin_UserManager_StudentManager : PowerCreator.LMS.Web.Base.BasePage
{
    public User opUser = new User();
    Department opDepart = new Department();
   
    PowerCreator.LMS.Web.Project.ProjectUser objProjectUser = new PowerCreator.LMS.Web.Project.ProjectUser();
    LogicServer.NetMeetingClass objNetMeetingClass;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //btnDel.Visible = IsLicensed("DelStudent");
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            ViewState["OrderList"] = "";  //
            ViewState["OrderType"] = " desc ";//排序方式
            BinderSex();
            BinderUserState();
            InitPage();
            BinderDepartmentClass();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_StudentManager.Text = GetLanageValueByKey("lbl_StudentManager_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StudentManager_Name.Text = GetLanageValueByKey("lbl_StudentManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StudentManager_LoadingName.Text = GetLanageValueByKey("lbl_StudentManager_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StudentManager_DepartmentName.Text = GetLanageValueByKey("lbl_StudentManager_DepartmentName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StudentManager_Sex.Text = GetLanageValueByKey("lbl_StudentManager_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_StateName.Text = GetLanageValueByKey("RoleUser_gv_c1", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Add.Text = GetLanageValueByKey("btn_StudentManager_AddStudent", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnLock.Text = GetLanageValueByKey("btnLock", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAuditing.Text = GetLanageValueByKey("btnAuditing", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_ExportUser.Text = GetLanageValueByKey("btn_ExportUser", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }
    private void BinderDepartmentClass()
    {
        string search = "DepartmentType = 2";
        DataTable dt = opDepart.Query(search);

        ddl_Department.DataSource = dt;
        ddl_Department.DataTextField = "DepartmentName";
        ddl_Department.DataValueField = "DepartmentId";
        ddl_Department.DataBind();

        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_Department.Items.Insert(0, li);
    }

    protected void BinderSex()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "2";
        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "0";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_Sex.Items.Add(li);
        ddl_Sex.Items.Add(li0);
        ddl_Sex.Items.Add(li1);
    }
    protected void BinderUserState()
    {
       
        ListItem li00 = new ListItem();
        li00.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li00.Value = "-1";
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("State_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "1";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("State_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";
        ddl_State.Items.Add(li00);
        ddl_State.Items.Add(li);
        ddl_State.Items.Add(li0);
        ddl_State.Items.Add(li1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn_del = (LinkButton)e.Item.FindControl("lnkbtn_del");
            LinkButton lbtn_upt = (LinkButton)e.Item.FindControl("lnkbtn_password");
            lbtn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            lbtn_upt.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_Upt_pwd_Msg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");
        }
    }
    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(txt_UserName.Text))
            {
                sbSearch.Append(string.Format("UserName like '%{0}%' and ", txt_UserName.Text));
            }
            if (!string.IsNullOrEmpty(txt_RealName.Text))
            {
                sbSearch.Append(string.Format("StudentName like '%{0}%'  and ", txt_RealName.Text));
            }
            if (ddl_Sex.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format("StudentSex = {0} and ", ddl_Sex.SelectedValue.ToString()));
            } 
            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format("FKStudentDepartmentId = {0} and ", ddl_Department.SelectedValue.ToString()));
            }
            if (ddl_State.SelectedValue.ToString() != "-1")
            {
                sbSearch.Append(string.Format("UserIscheck = {0} and ", ddl_State.SelectedValue.ToString()));
            }
        }
        else if (ViewState["SearchType"].ToString() == "0")
        {
            sbSearch.Append(string.Format("UserName like '%{0}%' or StudentName like '%{0}%' ", txt_SearchKey.Text));
        }

        if (sbSearch.ToString().EndsWith("and "))
            sbSearch.Remove(sbSearch.ToString().LastIndexOf("and "), 4);

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opUser.QueryVMAccountInfomationStudentCount(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderRPT(string search, int pageIndex)
    {
        string orderlist = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
            search += " order by " + orderlist + ViewState["OrderType"].ToString();
        DataTable dt = opUser.QueryVMAccountInfomaitionStudent(search, LMSPageSize, pageIndex);
        rptUser.DataSource = dt;
        rptUser.DataBind();
        PreAspNetPager(AspNetPager1);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        string url = "AddStudent.aspx";
        Script.RedirectURL(url);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btnAuditing_Click(object sender, EventArgs e)
    {
        //审核用户
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                VOTUser vo = opUser.LoadUser(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.UserIscheck != 1)
                    {
                        vo.UserIscheck = 1;
                        opUser.Update(vo);
                    }
                }
            }
        }
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //删除选中
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");

                DataTable dt_ProjectClassUser=objProjectUser.QueryProjectClassUser(" FKTrainclastuUserID=" + lblid.Text);

                int UserID = int.Parse(lblid.Text);
                int  TrainclastuID;
                for (int j = 0; j < dt_ProjectClassUser .Rows.Count; j++)
                {
                    TrainclastuID = int.Parse(dt_ProjectClassUser.Rows[j]["TrainclastuID"].ToString());
                    objProjectUser.DelProjectClassUser(TrainclastuID);
                }

                opUser.Del(UserID);

                if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
                {

                    objNetMeetingClass.WebDeleteUser(UserID);
                }
                

            }
        }
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btnLock_Click(object sender, EventArgs e)
    {
        //锁定选中
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {
                Label lblid = (Label)rptUser.Items[i].FindControl("lblid");
                VOTUser vo = opUser.LoadUser(int.Parse(lblid.Text));
                if (vo != null)
                {
                    if (vo.UserIscheck != 2)
                    {
                        vo.UserIscheck = 2;
                        opUser.Update(vo);
                    }
                }
            }
        }
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //取消
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "EditorStudent.aspx?StudentId=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //删除
            //if (IsLicensed("DelStudent"))
            //{
                PowerCreator.LMS.User.Entity.VOTStudent voStudent = opUser.LoadStudent(int.Parse(e.CommandArgument.ToString()));

                DataTable dt_ProjectClassUser = objProjectUser.QueryProjectClassUser(" FKTrainclastuUserID=" + voStudent.FKStudentUserId.ToString());

                int TrainclastuID;
                for (int j = 0; j < dt_ProjectClassUser.Rows.Count; j++)
                {
                    TrainclastuID = int.Parse(dt_ProjectClassUser.Rows[j]["TrainclastuID"].ToString());
                    objProjectUser.DelProjectClassUser(TrainclastuID);
                }
                if (opUser.Del(voStudent.FKStudentUserId))
                {
                    if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
                    {

                        objNetMeetingClass.WebDeleteUser(voStudent.FKStudentUserId);
                    }
                    string search = GetSearch();
                    BinderPager(search);
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
            //}
            //else
            //{
            //    Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
            //    lbl_msg.Text = GetLanageValueByKey("StudentManager_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}

            //string url = "DeleteStudent.aspx?StudentId=" + e.CommandArgument.ToString() + "&Url=StudentManager.aspx";
            //Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "lauditing")
        {
            //审核
            VOTUser voUser = opUser.LoadUser(int.Parse(e.CommandArgument.ToString()));
            if (voUser != null)
            {
                voUser.UserIscheck += 1;
                if (voUser.UserIscheck >= 3)
                    voUser.UserIscheck = 0;

                if (opUser.Update(voUser))
                {
                    string search = GetSearch();
                    BinderRPT(search, AspNetPager1.CurrentPageIndex);
                }
            }
        }
        else if (e.CommandName.ToLower() == "lpassword")
        {
            //初始化密码
            VOTStudent voStudent = opUser.LoadStudent(int.Parse(e.CommandArgument.ToString()));
            VOTUser voUser = opUser.LoadUser(voStudent.FKStudentUserId);
            //123456默认密码
            voUser.UserPassword = "E10ADC3949BA59ABBE56E057F20F883E";
            opUser.Update(voUser);
        }
        else if (e.CommandName == "orderby")
        {
            ViewState["OrderList"] = e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
    protected void btn_ExportUser_Click(object sender, EventArgs e)
    {
        string url = "UserExportByExcel.aspx?UserType=0";

        Script.RedirectURL(url);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";  //0简单搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
}
